package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_shippingController {

	private static Logger logger = Logger.getLogger(T_shippingController.class);

	@Autowired
	private ApiT_shippingService apiT_shippingService;
	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private ApiT_shipper_accountService apiT_shipper_accountService;
	@Autowired
	private ApiCommonService apiCommonService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping_list")
	public ModelAndView t_shipping_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_list.jsp");

		List<Shipping_list> t_shippings = apiT_shippingService
				.htshippingsListPage(pageInfo);
		mv.addObject("t_shippings", t_shippings);
		mv.addObject("status",-1);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	//待派送列表
	@RequestMapping(value = "t_shipping-wait-list")
	public ModelAndView t_shipping_list_wait(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_wait_list.jsp");

		List<Shipping_list> t_shippings = apiT_shippingService
				.htshippingswaitListPage(pageInfo);
		mv.addObject("t_shippings", t_shippings);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	
	//多条件查询列表
	@RequestMapping(value = "t_shipping_listbyterm")
	public ModelAndView t_shipping_listbyterm(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		Map<String,Object> conditions = new HashMap<String,Object>();
		String status = request.getParameter("status");
		String titles = request.getParameter("titles");
		String mobile = request.getParameter("mobile");
		String ordertype = request.getParameter("ordertype");
		String zf_type = request.getParameter("zf_type");
		String c_tname = request.getParameter("c_tname");
		String name = request.getParameter("name");
		String sendDate1 = request.getParameter("sendDate1");
		String sendDate2 = request.getParameter("sendDate2");
		String app_date1 = request.getParameter("app_date1");
		String app_date2 = request.getParameter("app_date2");
		String message = request.getParameter("message");
		request.setAttribute("message", message);
		request.setAttribute("status", status);
		request.setAttribute("titles", titles);
		request.setAttribute("mobile", mobile);
		request.setAttribute("ordertype", ordertype);
		request.setAttribute("zf_type", zf_type);
		request.setAttribute("c_tname", c_tname);
		request.setAttribute("name", name);
		request.setAttribute("sendDate1", sendDate1);
		request.setAttribute("sendDate2", sendDate2);
		request.setAttribute("app_date1", app_date1);
		request.setAttribute("app_date2", app_date2);
		conditions.put("status", status);
		conditions.put("titles", titles);
		conditions.put("mobile", mobile);
		conditions.put("ordertype", ordertype);
		conditions.put("zf_type", zf_type);
		conditions.put("c_tname", c_tname);
		conditions.put("name", name);
		conditions.put("sendDate1", sendDate1);
		conditions.put("sendDate2", sendDate2);
		conditions.put("app_date1", app_date1);
		conditions.put("app_date2", app_date2);
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_list.jsp");

		List<Shipping_list> t_shippings = apiT_shippingService
				.htshippingsbytermListPage(conditions,pageInfo);
		mv.addObject("t_shippings", t_shippings);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	//多条件查询待派送列表
	@RequestMapping(value = "t_shipping_wait_listbyterm")
	public ModelAndView t_shipping_wait_listbyterm(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		Map<String,Object> conditions2 = new HashMap<String,Object>();
		String titles = request.getParameter("titles");
		String mobile = request.getParameter("mobile");
		String ordertype = request.getParameter("ordertype");
		String zf_type = request.getParameter("zf_type");
		String c_tname = request.getParameter("c_tname");
		String name = request.getParameter("name");
		String sendDate1 = request.getParameter("sendDate1");
		String sendDate2 = request.getParameter("sendDate2");
		String app_date1 = request.getParameter("app_date1");
		String app_date2 = request.getParameter("app_date2");
		String message = request.getParameter("message");
		request.setAttribute("message", message);
		request.setAttribute("titles", titles);
		request.setAttribute("mobile", mobile);
		request.setAttribute("ordertype", ordertype);
		request.setAttribute("zf_type", zf_type);
		request.setAttribute("c_tname", c_tname);
		request.setAttribute("name", name);
		request.setAttribute("sendDate1", sendDate1);
		request.setAttribute("sendDate2", sendDate2);
		request.setAttribute("app_date1", app_date1);
		request.setAttribute("app_date2", app_date2);
		conditions2.put("titles", titles);
		conditions2.put("mobile", mobile);
		conditions2.put("ordertype", ordertype);
		conditions2.put("zf_type", zf_type);
		conditions2.put("c_tname", c_tname);
		conditions2.put("name", name);
		conditions2.put("sendDate1", sendDate1);
		conditions2.put("sendDate2", sendDate2);
		conditions2.put("app_date1", app_date1);
		conditions2.put("app_date2", app_date2);
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_wait_list.jsp");

		List<Shipping_list> t_shippings = apiT_shippingService
				.htshippingwaitbytermListPage(conditions2,pageInfo);
		mv.addObject("t_shippings", t_shippings);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-add-pre")
	public ModelAndView t_shipping_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_shipping t_shipping = apiT_shippingService
					.findT_shippingById(Integer.valueOf(id));
			mv.addObject("t_shipping", t_shipping);
		}
		response.setCharacterEncoding("UTF-8");
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-add")
	public void t_shipping_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String shipping_id = request.getParameter("shipping_id");
		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String shipping_price = request.getParameter("shipping_price");
		String status = request.getParameter("status");
		String shipping_remark = request.getParameter("shipping_remark");
		String create_time = request.getParameter("create_time");
		String allocating_time = request.getParameter("allocating_time");
		String arrive_shop_time = request.getParameter("arrive_shop_time");
		String take_goods_time = request.getParameter("take_goods_time");
		String shipping_out_time = request.getParameter("shipping_out_time");
		String arrive_user_time = request.getParameter("arrive_user_time");
		String receive_order_time = request.getParameter("receive_order_time");
		String start_time = request.getParameter("start_time");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_shipping t_shipping = new ApiT_shipping();

			t_shipping.setOrder_id(order_id);
			t_shipping.setUser_id(user_id);
			// t_shipping.setShipping_price(Integer.valueOf(shipping_price));
			t_shipping.setStatus(Integer.valueOf(status));
			// t_shipping.setShipping_remark(shipping_remark);
			/*
			 * t_shipping.setCreate_time(create_time);
			 * t_shipping.setAllocating_time(allocating_time);
			 * t_shipping.setArrive_shop_time(arrive_shop_time);
			 * t_shipping.setTake_goods_time(take_goods_time);
			 * t_shipping.setShipping_out_time(shipping_out_time);
			 * t_shipping.setArrive_user_time(arrive_user_time);
			 * t_shipping.setReceive_order_time(receive_order_time);
			 * t_shipping.setStart_time(start_time);
			 */

			apiT_shippingService.insertT_shipping(t_shipping);
		}
		// edit
		else {
			ApiT_shipping t_shipping = apiT_shippingService
					.findT_shippingById(Integer.valueOf(id));

			t_shipping.setOrder_id(order_id);
			t_shipping.setUser_id(user_id);
			/*
			 * t_shipping.setShipping_price(shipping_price);
			 * t_shipping.setStatus(status);
			 * t_shipping.setShipping_remark(shipping_remark);
			 * t_shipping.setCreate_time(create_time);
			 * t_shipping.setAllocating_time(allocating_time);
			 * t_shipping.setArrive_shop_time(arrive_shop_time);
			 * t_shipping.setTake_goods_time(take_goods_time);
			 * t_shipping.setShipping_out_time(shipping_out_time);
			 * t_shipping.setArrive_user_time(arrive_user_time);
			 * t_shipping.setReceive_order_time(receive_order_time);
			 * t_shipping.setStart_time(start_time);
			 */

			apiT_shippingService.updateT_shipping(t_shipping);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-delete")
	public void t_shipping_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_shippingService.deleteT_shipping(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 已分配订单列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-already-list")
	public ModelAndView t_shipping_already_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_already_list.jsp");

		List<ApiT_shipping> t_shippings = apiT_shippingService
				.status(1);
		mv.addObject("t_shippings", t_shippings);

		return mv;
	}
	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-allocate-pre")
	public ModelAndView t_shipping_allocate_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipping_allocate.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_shipping t_shipping = apiT_shippingService
					.findT_shippingById(Integer.valueOf(id));
			mv.addObject("t_shipping", t_shipping);

			// 0用户 1 绑定商户 2 vip 9被屏蔽 3 配送员
			List<Map<String, Object>> t_shippers = apiT_shipper_accountService
					.selectShipperAtWork();
			mv.addObject("t_members", t_shippers);
		}
		return mv;
	}

	/**
	 * 手动分配
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipping-allocate")
	public void t_shipping_allocate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String status = request.getParameter("status");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id != null || !(id.trim()).equals("")) {
			apiCommonService.allocateOrderTaskManually(order_id, user_id);

			// t_shipping.setOrder_id(order_id);
			// t_shipping.setUser_id(user_id);
			// apiT_shippingService.updateT_shipping(t_shipping);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "分配成功！");

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	//后台手动派单
	@RequestMapping(value = "t_shipping-allocate_ht")
	public void t_shipping_allocate_ht(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String status = request.getParameter("status");

		String id = request.getParameter("id");
		System.out.println("-----------------------------显示数据：id="+id+",order_id="+order_id+",user_id"+user_id+",status"+status);
		// add
		if (id != null || !(id.trim()).equals("")) {
			apiCommonService.allocateOrderTaskManually(order_id, user_id);

			// t_shipping.setOrder_id(order_id);
			// t_shipping.setUser_id(user_id);
			// apiT_shippingService.updateT_shipping(t_shipping);
		}
		//
	}
	
	//更改派单
	@RequestMapping(value = "t_shipping_change_ht")
	public void t_shipping_change_ht(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String status = request.getParameter("status");

		String id = request.getParameter("id");
		// 
		if (id != null || !(id.trim()).equals("")) {
			apiCommonService.changeallocateOrder(Integer.parseInt(id), order_id, user_id);

			// t_shipping.setOrder_id(order_id);
			// t_shipping.setUser_id(user_id);
			// apiT_shippingService.updateT_shipping(t_shipping);
		}
		//
	}
	
	//立即派单配送员列表
	@RequestMapping(value = "Shippers_pd")
	public ModelAndView Shippers_pd(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		String shipping_id = request.getParameter("shipping_id");
		String titles=request.getParameter("titles");
		String thetype=request.getParameter("thetype");
		request.setAttribute("shipping_id", shipping_id);
		request.setAttribute("titles", titles);
		request.setAttribute("thetype", thetype);
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipper_pd.jsp");

		List<Shipper_list> shippers = apiT_shipper_accountService.Shippers_pd(titles,pageInfo);
		mv.addObject("shippers", shippers);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}
	
	//更改派单配送员列表
	@RequestMapping(value = "Shippers_change_pd")
	public ModelAndView Shippers_change_pd(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception {
		String shipping_id = request.getParameter("shipping_id");
		String titles=request.getParameter("titles");
		String thetype=request.getParameter("thetype");
		request.setAttribute("shipping_id", shipping_id);
		request.setAttribute("titles", titles);
		request.setAttribute("thetype", thetype);
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_shipper_change_pd.jsp");

		List<Shipper_list> shippers = apiT_shipper_accountService.Shippers_change_pd(shipping_id,titles, pageInfo);
		mv.addObject("shippers", shippers);
		return mv;
	}
}
